﻿using Microsoft.Extensions.Logging;
using Orleans;
using Orleans.Runtime.Configuration;
using System;
using System.Threading;
using System.Threading.Tasks;
using Zj.OrleansDemo.Interface;

namespace Zj.OrleansDemo.ConsoleView
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Thread.Sleep(10000);
                var client = RunAsync(10086).Result;
                while (true)
                {
                    Console.WriteLine("Press any key to start:");
                    Console.ReadLine();
                    var _service = client.GetGrain<ITestService>(0);
                    _service.TestAsync();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadLine();
        }

        private static async Task<IClusterClient> RunAsync(int gatewayPort=40000)
        {
            var cfg = ClientConfiguration.LocalhostSilo(gatewayPort);
            var builder = new ClientBuilder()
                .UseConfiguration(cfg)
                .ConfigureApplicationParts(parts => parts.AddFromApplicationBaseDirectory().WithReferences());
            var client = builder.Build();
            await client.Connect();
            return client;
        }
    }
}
